<?php
namespace api\modules\shop\controllers;


use common\models\SysHotWord;
use common\models\UserHotWord;
use common\service\ConstantService;

class HotWordsController extends ShopBaseController
{
    /**
     * 添加热搜词
     */
    public function actionAddHotWord(){
        if($this->isGet()){
            //搜索关键词
            $search_type = $this->get('search_type'); // 1 用户 2 系统
            $search_word = $this->get('search_word');
            if(empty($search_type) || empty($search_word)){
                return $this->renderJson([],'参数错误',ConstantService::$response_fail);
            }
            if($search_type == 1){ //用户热词
                $user_id = $this->shop['id'];
                $word = UserHotWord::findOne(['word'=>$search_word,'type'=>1,'is_del' => 0,'user_id' => $user_id]);
                if(!empty($word)){
                    $word->search_times = $word['search_times'] + 1;
                    $word->update_time = time();
                    $re = $word->save();
                }else{
                    $data['word'] = $search_word;
                    $data['search_times'] = 1;
                    $data['user_id'] = $user_id;
                    $data['is_del'] = 0;
                    $data['type'] = 1;
                    $data['create_time'] = time();
                    $data['update_time'] = time();
                    $word_query = new UserHotWord();
                    $word_query->setAttributes($data);
                    $re = $word_query->save();
                }
            }else{
                //系统热词
                $word = SysHotWord::findOne(['word'=>$search_word,'type'=>1,'is_del' => 0]);
                $word->search_times = $word['search_times'] + 1;
                $word->update_time = time();
                $re = $word->save();
            }
            if($re){
                return $this->renderJson([],'保存成功',ConstantService::$response_success);
            }
            return $this->renderJson([],'操作失败',ConstantService::$response_fail);
        }
        return $this->renderJson([],'参数错误',ConstantService::$response_fail);

    }

    /**
     * 获取系统热词
     */
    public function actionGetHotWords(){
        if($this->isGet()){
            $lists = SysHotWord::find()
                ->where(['is_del' => 0,'type'=>1])
                ->orderBy(['search_times' => SORT_DESC])
                ->asArray()
                ->all();
            return $this->renderJson($lists,'',ConstantService::$response_success);
        }
        return $this->renderJson([],'参数错误',ConstantService::$response_fail);
    }

    /**
     * 获取用户热词
     */
    public function actionGetUserWords(){
        if($this->isGet()){
            $conditions['is_del'] = 0;
            $conditions['type'] = 1;
            $conditions['user_id'] = $this->shop['id'];
            $lists = UserHotWord::find()
                ->where($conditions)
                ->orderBy(['update_time' => SORT_DESC])
                ->offset(0)
                ->limit(12)
                ->asArray()
                ->all();
            return $this->renderJson($lists,'',ConstantService::$response_success);
        }
        return $this->renderJson([],'参数错误',ConstantService::$response_fail);
    }

    /**
     * 删除用户热词
     */
    public function actionDelUserWords(){
        if($this->isGet()){
            $user_id = $this->shop['id'];
            $re = UserHotWord::updateAll(['is_del'=>1],['user_id'=>$user_id,'type'=>1]);
            if($re){
                return $this->renderJson([],'删除成功!',ConstantService::$response_success);
            }
            return $this->renderJson([],'删除失败',ConstantService::$response_fail);


        }
        return $this->renderJson([],'参数错误',ConstantService::$response_fail);

    }

}